package com.myapp.actions.user;

import com.myweb.WebAction;
import com.myweb.WebException;
import com.myweb.WebRequest;
import com.myweb.WebResponse;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

public class ListUsers implements WebAction {

  private String query;
  private int start;
  private int length;
    
  @Override
  public void prepare(WebRequest req) throws WebException {
      query = req.getString("query");
      start = req.getInteger("start");
      length = req.getInteger("length");
  }

 @Override
    public EntityManager process(WebRequest req, WebResponse res, EntityManager em) throws Exception {
    Query q = em.createQuery("SELECT u FROM Uzer u WHERE u.deleted = false AND u.username LIKE :un");
    q.setFirstResult(start);
    q.setMaxResults(length);
    q.setParameter("un", "%" + query + "%");
    res.setResult(q.getResultList());
    return em;
  }

}
